package com.example.demo.actor;

import akka.actor.AbstractActor;
import com.example.demo.model.Message;
import com.example.demo.service.BusinessService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.concurrent.CompletableFuture;

/**
 * Created by Administrator on 2018/2/7.
 */
@Component
@Scope("prototype")
public class WorkerActor extends AbstractActor {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private BusinessService businessService;

    final private CompletableFuture<Message> future;

    public WorkerActor(CompletableFuture<Message> future) {
        this.future = future;
    }


    @Override
    public Receive createReceive() {
        return receiveBuilder()
                .match(Message.class, message -> {
                    businessService.perform(message);
                    future.complete(message);
                })
                .matchAny(o -> logger.info("received unknown message"))
                .build();
    }

}
